package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.zhijian.medical.dao.local.ExperimentProcessSpecimenMapper;
import com.zhijian.medical.entity.dto.PcrResultDto;
import com.zhijian.medical.entity.po.*;
import com.zhijian.medical.entity.vo.request.ExperimentProcessSpecimenSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSpecimenBatchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessSpecimenVo;
import com.zhijian.medical.service.*;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.BeanUtil;
import com.zhijian.medical.util.Page;
import com.zhijian.medical.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 实验流程-临床标本标识和扩增位置业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-04-09 17:11:14
 */
@Slf4j
@Service
public class ExperimentProcessSpecimenServiceImpl implements ExperimentProcessSpecimenService {

    @Autowired
    private ExperimentProcessSpecimenMapper mapper;

    @Autowired
    private ExperimentProcessResultService experimentProcessResultService;

    @Autowired
    private ExperimentProcessResultDetailService experimentProcessResultDetailService;

    @Autowired
    private ExperimentProcessService experimentProcessService;

    @Autowired
    private DeviceService deviceService;

    @Override
    public ExperimentProcessSpecimen selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExperimentProcessSpecimenVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(ExperimentProcessSpecimen experimentProcessSpecimen) {
        Date now = new Date();
        experimentProcessSpecimen.setCreateTime(now);
        experimentProcessSpecimen.setUpdateTime(now);
        return mapper.insert(experimentProcessSpecimen) > 0 ? experimentProcessSpecimen.getId() : 0L;
    }

    @Override
    public boolean insertBySelective(Map<String, List<String>> param) {
        mapper.insertBySelective(param);
        return true;
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public Long insertOrUpdate(ExperimentProcessSpecimen experimentProcessSpecimen) {
        Long experimentProcessId = experimentProcessSpecimen.getExperimentProcessId();
        this.deleteByExperimentProcessId(experimentProcessId);
        return this.insert(experimentProcessSpecimen);
    }

    @Override
    public boolean update(ExperimentProcessSpecimen experimentProcessSpecimen) {
        Date now = new Date();
        experimentProcessSpecimen.setUpdateTime(now);
        return mapper.updateByPrimaryKey(experimentProcessSpecimen) != 0;
    }

    @Override
    public Integer count(ExperimentProcessSpecimenSearchVo experimentProcessSpecimenSearchVo) {
        return mapper.count(experimentProcessSpecimenSearchVo);
    }

    @Override
    public List<ExperimentProcessSpecimenVo> selectList(ExperimentProcessSpecimenSearchVo experimentProcessSpecimenSearchVo) {
        return mapper.selectList(experimentProcessSpecimenSearchVo);
    }

    @Override
    public Page<ExperimentProcessSpecimenVo> selectPage(ExperimentProcessSpecimenSearchVo experimentProcessSpecimenSearchVo) {
        //先直接查总数
        int count = count(experimentProcessSpecimenSearchVo);
        //分页
        Page<ExperimentProcessSpecimenVo> page = new Page<>(true, count, experimentProcessSpecimenSearchVo.getPageNum(), experimentProcessSpecimenSearchVo.getPageSize());
        experimentProcessSpecimenSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExperimentProcessSpecimenVo> responseVos = this.selectList(experimentProcessSpecimenSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public void deleteByExperimentProcessId(Long experimentProcessId) {
        mapper.deleteByExperimentProcessId(experimentProcessId);
    }

    @Override
    public void deleteByExperimentProcessIdAndDeviceId(Long experimentProcessId, Long deviceId) {
        mapper.deleteByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
    }

    @Override
    public ExperimentProcessSpecimenVo selectVoByExperimentProcessId(Long experimentProcessId) {
        return mapper.selectVoByExperimentProcessId(experimentProcessId);
    }

    @Override
    public ExperimentProcessSpecimenVo selectVoByExperimentProcessIdAndDeviceId(Long experimentProcessId, Long deviceId) {
        return mapper.selectVoByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
    }

    @Override
    public ExperimentProcessSpecimenBatchVo selectExperimentProcessSpecimenBatchVoByExperimentProcessId(Long experimentProcessId) {
        ExperimentProcessSpecimenVo experimentProcessSpecimenVo = this.selectVoByExperimentProcessId(experimentProcessId);
        if (Objects.isNull(experimentProcessSpecimenVo)) {
            return null;
        }
        //把实体类转化成map
        Map<String, Object> map = BeanUtil.objToMap(experimentProcessSpecimenVo, true);
        //找到a开头的元素，并且按照大小进行排序，把所有的值排成个list，以下同理
        List<String> a = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("a")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> b = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("b")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> c = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("c")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> d = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("d")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        //这里判断了下key的长度，是去除实验流程id这个key
        List<String> e = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("e") && e1.getKey().length() <= 3).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> f = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("f")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> g = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("g")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> h = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("h")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        return ExperimentProcessSpecimenBatchVo.builder()
                .a(a)
                .b(b)
                .c(c)
                .d(d)
                .e(e)
                .f(f)
                .g(g)
                .h(h)
                .build();
    }

    @Override
    public ExperimentProcessSpecimenBatchVo selectExperimentProcessSpecimenBatchVoByExperimentProcessIdAndDeviceId(Long experimentProcessId, Long deviceId) {
        ExperimentProcessSpecimenVo experimentProcessSpecimenVo = this.selectVoByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
        if (Objects.isNull(experimentProcessSpecimenVo)) {
            return null;
        }
        //把实体类转化成map
        Map<String, Object> map = BeanUtil.objToMap(experimentProcessSpecimenVo, true);
        //找到a开头的元素，并且按照大小进行排序，把所有的值排成个list，以下同理
        List<String> a = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("a")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> b = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("b")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> c = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("c")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        //去除设备id
        List<String> d = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("d") && e1.getKey().length() <= 3).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        //这里判断了下key的长度，是去除实验流程id这个key
        List<String> e = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("e") && e1.getKey().length() <= 3).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> f = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("f")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> g = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("g")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        List<String> h = map.entrySet().stream().filter(e1 -> e1.getKey().startsWith("h")).sorted(Comparator.comparing(Map.Entry::getKey, ExperimentProcessSpecimenServiceImpl::compare)).map(Map.Entry::getValue).collect(Collectors.toList()).stream().map(a1 -> Objects.isNull(a1) ? null : String.valueOf(a1)).collect(Collectors.toList());
        return ExperimentProcessSpecimenBatchVo.builder()
                .a(a)
                .b(b)
                .c(c)
                .d(d)
                .e(e)
                .f(f)
                .g(g)
                .h(h)
                .build();
    }

    public static int compare(String a, String b) {
        Integer aNumber = StrUtil.drawNumberFromStr(a);
        Integer bNumber = StrUtil.drawNumberFromStr(b);
        return Integer.compare(aNumber, bNumber);
    }

    @Transactional(rollbackFor = Exception.class, value = "localTransactionManager")
    @Override
    public boolean importExcel(Long experimentProcessId, Long deviceId, MultipartFile file) {
        ExperimentProcess experimentProcess = experimentProcessService.selectById(experimentProcessId);
        AssertUtil.notNull(experimentProcess, "此实验流程不存在或者已被删除");
        Device device = deviceService.selectById(deviceId);
        AssertUtil.notNull(device, "此扩增仪不存在或者已被删除");
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        String fileName = file.getOriginalFilename();
//        if (fileName.contains(ExperimentProcessTypeEnum.KRAS.getDesc())) {
//            fileName = ExperimentProcessTypeEnum.KRAS.getDesc();
//        } else if (fileName.contains(ExperimentProcessTypeEnum.NRAS.getDesc())) {
//            fileName = ExperimentProcessTypeEnum.NRAS.getDesc();
//        } else if (fileName.contains(ExperimentProcessTypeEnum.BRAF.getDesc())) {
//            fileName = ExperimentProcessTypeEnum.BRAF.getDesc();
//        } else {
//            throw new BusinessException(ResponseCodeEnum.FAILURE.code, "文件名称无法分辨");
//        }

        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<List<Object>> listList = reader.read(8);
        TreeSet<String> keys = Sets.newTreeSet();
        List<String> values = Lists.newLinkedList();
        //先查询下有没有
        ExperimentProcessSpecimenVo experimentProcessSpecimenVo = this.selectVoByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
        if (Objects.nonNull(experimentProcessSpecimenVo)) {
            //先直接删掉
            this.deleteByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
            //因为可能会有多个Excel的数据，组成一个实验流程所有的标本数据，所以之前有的话，先把值筛出来
            Class clazz;
            try {
                //反射
                clazz = Class.forName("com.zhijian.medical.entity.vo.response.ExperimentProcessSpecimenVo");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            //获得这个类的所有值
            Field[] fields = clazz.getDeclaredFields();
            Arrays.stream(fields).forEach(field -> {
                //获取私有化属性，先设置
                field.setAccessible(true);
                //字段名称
                String key = field.getName();
                //这俩不需要代入
                if (!"id".equals(key) && !("experimentProcessId").equals(key) && !("deviceId").equals(key)) {
                    String value;
                    try {
                        //字段对应的值
                        value = (String) field.get(experimentProcessSpecimenVo);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                        return;
                    }
                    if (StringUtils.isNotBlank(value)) {
                        //先加入一会要插入数据的两个collection
                        if (keys.add(key.toUpperCase())) {
                            values.add(value);
                        }
                    }
                }
            });
        }
        Set<String> valueSet = Sets.newHashSet();

        Set<String> keySet = Sets.newHashSet();
        listList.forEach(l -> {
            if (keys.add((String) l.get(0))) {
                keySet.add((String) l.get(0));
                values.add((String) l.get(1));
            }
            valueSet.add((String) l.get(1));
        });
        if (keys.add("device_id")) {
            values.add(deviceId + "");
        }
        if (keys.add("experiment_process_id")) {
            values.add(experimentProcessId + "");
        }
        Map<String, List<String>> param = Maps.newHashMap();
        param.put("key", CollectionUtil.newArrayList(keys));
        param.put("value", values);
        this.insertBySelective(param);


        //根据实验流程id与项目名进行删除
        experimentProcessResultService.deleteByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);

        List<PcrResultDto.OneRowDto> oneRowDtoList = listList.stream().filter(l -> StringUtils.isNotBlank((String) l.get(1))).map(l -> PcrResultDto.OneRowDto.builder()
                .fyk((String) l.get(0))
                .ybmc((String) l.get(1))
                .jymc((String) l.get(2))
                .task((String) l.get(3))
                .bgjy((String) l.get(4))
                .cmjy((String) l.get(5))
                .ct(StringUtils.isNotBlank(l.get(6) + "") && !"未确定".equals(l.get(6) + "") ? new BigDecimal(l.get(6) + "") : BigDecimal.ZERO)
                .ctpjz(StringUtils.isNotBlank(l.get(7) + "") ? new BigDecimal(l.get(7) + "") : BigDecimal.ZERO)
                .ctsd(StringUtils.isNotBlank(l.get(8) + "") ? new BigDecimal(l.get(8) + "") : BigDecimal.ZERO)
                .sl(StringUtils.isNotBlank(l.get(9) + "") ? Integer.valueOf(l.get(9) + "") : 0)
                .slpjz(StringUtils.isNotBlank(l.get(10) + "") ? Integer.valueOf(l.get(10) + "") : 0)
                .slsd(StringUtils.isNotBlank(l.get(11) + "") ? Integer.valueOf(l.get(11) + "") : 0)
                .zdctyz(l.get(12))
                .ctyz(StringUtils.isNotBlank(l.get(13) + "") ? new BigDecimal(l.get(13) + "") : BigDecimal.ZERO)
                .zdjx(l.get(14))
                .jxqd(StringUtils.isNotBlank(l.get(15) + "") ? Integer.valueOf(l.get(15) + "") : 0)
                .jxzd(StringUtils.isNotBlank(l.get(16) + "") ? Integer.valueOf(l.get(16) + "") : 0)
                .zs((String) l.get(17))
                .highsd((String) l.get(18))
                .build()).collect(Collectors.toList());


        List<PcrResultDto> d1 = valueSet.stream().map(v3 -> {
            PcrResultDto pcrResultDto = new PcrResultDto();
            pcrResultDto.setYbmc(v3);
            List<PcrResultDto.OneRowDto> collect = oneRowDtoList.stream().filter(o -> o.getYbmc().equals(v3)).collect(Collectors.toList());
            Set<String> fykSet = Sets.newHashSet();
            collect.forEach(c -> fykSet.add(c.getFyk()));
            List<PcrResultDto.FYKDto> fykDtoList = Lists.newArrayList();
            fykSet.forEach(f -> {
                List<PcrResultDto.OneRowDto> collect1 = collect.stream().filter(o -> o.getFyk().equals(f)).collect(Collectors.toList());
                PcrResultDto.FYKDto fykDto = new PcrResultDto.FYKDto();
                fykDto.setFyk(f);
                fykDto.setOneRowDtoList(collect1);
                fykDtoList.add(fykDto);
            });
            pcrResultDto.setFykDtoList(fykDtoList);
            return pcrResultDto;
        }).collect(Collectors.toList());


        d1.forEach(d -> {
            List<PcrResultDto.FYKDto> fykDtoList = d.getFykDtoList();
            ExperimentProcessResult experimentProcessResult = new ExperimentProcessResult();
            experimentProcessResult.setExperimentProcessId(experimentProcessId);
            experimentProcessResult.setDeviceId(deviceId);
            //样本名
            String ybmc = d.getYbmc();
            String[] ybmcAry = ybmc.split("-");
            AssertUtil.check(ybmcAry.length < 2, "有不符合的的样本名，请检查后重写填写");
            experimentProcessResult.setProject(ybmcAry[0]);
            experimentProcessResult.setBlh(ybmcAry[1]);
            Long resultId = experimentProcessResultService.insert(experimentProcessResult);
            Set<String> collect = fykDtoList.stream().map(PcrResultDto.FYKDto::getFyk).collect(Collectors.toSet());
            List<ExperimentProcessResultDetail> collect1 = collect.stream().map(c -> {
                ExperimentProcessResultDetail experimentProcessResultDetail = new ExperimentProcessResultDetail();
                experimentProcessResultDetail.setResultId(resultId);
                experimentProcessResultDetail.setExperimentProcessId(experimentProcessId);
                experimentProcessResultDetail.setDeviceId(deviceId);
                experimentProcessResultDetail.setFyk(c);
                List<PcrResultDto.FYKDto> k1 = fykDtoList.stream().filter(f -> StringUtils.isNotBlank(f.getFyk()) && f.getFyk().equals(c)).collect(Collectors.toList());
                PcrResultDto.FYKDto fykDto = k1.get(0);
                PcrResultDto.OneRowDto oneRowDto1 = fykDto.getOneRowDtoList().get(0);
                PcrResultDto.OneRowDto oneRowDto2 = fykDto.getOneRowDtoList().get(1);
                //内控CT-> KRAS excel里面的基因名称是VIC的，G列Ct值
                if (oneRowDto1.getJymc().equals("VIC")) {
                    experimentProcessResultDetail.setNkct(oneRowDto1.getCt());
                } else if (oneRowDto2.getJymc().equals("VIC")) {
                    experimentProcessResultDetail.setNkct(oneRowDto2.getCt());
                }
                if (StrUtil.drawNumberFromStr(c) == 12) {
                    //外控CT-> A12excel里面的基因名称是FAM的，G列Ct值
                    if (oneRowDto1.getJymc().equals("FAM")) {
                        experimentProcessResultDetail.setWkct(oneRowDto1.getCt());
                    } else if (oneRowDto2.getJymc().equals("FAM")) {
                        experimentProcessResultDetail.setWkct(oneRowDto2.getCt());
                    }
                } else {
                    //突变CT-> A1 - A11, 基因名称是FAM Ct(G列)
                    if (oneRowDto1.getJymc().equals("FAM")) {
                        experimentProcessResultDetail.setTbct(oneRowDto1.getCt());
                    } else if (oneRowDto2.getJymc().equals("FAM")) {
                        experimentProcessResultDetail.setTbct(oneRowDto2.getCt());
                    }
                }
                return experimentProcessResultDetail;
            }).collect(Collectors.toList());
            //拿到A12 行的外控CT值，赋值给其他所有的
            List<BigDecimal> bigDecimals = collect1.stream().filter(c -> Objects.nonNull(c.getWkct())).map(ExperimentProcessResultDetail::getWkct).collect(Collectors.toList());
            List<ExperimentProcessResultDetail> collect2;
            if (CollectionUtils.isEmpty(bigDecimals)) {
                collect2 = collect1.stream().map(c -> {
                    //突变ct
                    BigDecimal tbct = c.getTbct();
                    //28>突变ct>25，此时需要计算ΔCt值，ΔCt值=突变ct-外控ct
                    BigDecimal bigDecimal28 = new BigDecimal("28");
                    BigDecimal bigDecimal25 = new BigDecimal("25");
                    if (bigDecimal28.compareTo(tbct) == 1 && tbct.compareTo(bigDecimal25) == 1) {
                        BigDecimal wkct = c.getWkct();
                        //德尔塔CT值
                        BigDecimal ct3 = tbct.subtract(wkct);
                        c.setCt3(ct3);
                    }
                    return c;
                }).collect(Collectors.toList());
            } else {
                collect2 = collect1;
            }
            List<ExperimentProcessResultDetail> collect3 = collect2.stream().map(c -> {
                String fyk = c.getFyk();
                //突变ct
                BigDecimal tbct = c.getTbct();
                BigDecimal bigDecimal26 = new BigDecimal("26");
                BigDecimal bigDecimal28 = new BigDecimal("28");
                BigDecimal bigDecimal29 = new BigDecimal("29");
                BigDecimal bigDecimal9 = new BigDecimal("9");
                BigDecimal bigDecimal10 = new BigDecimal("10");
                BigDecimal bigDecimal11 = new BigDecimal("11");
                //德尔塔ct
                BigDecimal ct3 = c.getCt3();
                if (Objects.isNull(tbct)) {
                    return c;
                }
                if (tbct.compareTo(bigDecimal26) == -1) {
                    //所有的突变ct，如果小于26，结果就是强
                    c.setYyx("强");
                } else {
                    //反应孔坐标
                    Integer fykPosition = StrUtil.drawNumberFromStr(fyk);
                    switch (fykPosition) {
                        case 1:
                            if (tbct.compareTo(bigDecimal28) == 0 || tbct.compareTo(bigDecimal28) == 1) {
                                //当突变ct>=28的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal28) == -1 &&
                                    (ct3.compareTo(bigDecimal9) == 0 || ct3.compareTo(bigDecimal9) == -1)) {
                                //当26<=突变ct<28，并且德尔塔ct<=9的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 2:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal10) == 0 || ct3.compareTo(bigDecimal10) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=10的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 3:
                            if (tbct.compareTo(bigDecimal28) == 0 || tbct.compareTo(bigDecimal28) == 1) {
                                //当突变ct>=28的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal28) == -1 &&
                                    (ct3.compareTo(bigDecimal11) == 0 || ct3.compareTo(bigDecimal11) == -1)) {
                                //当26<=突变ct<28，并且德尔塔ct<=11的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 4:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal10) == 0 || ct3.compareTo(bigDecimal10) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=10的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 5:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal9) == 0 || ct3.compareTo(bigDecimal9) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=9的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 6:
                            if (tbct.compareTo(bigDecimal28) == 0 || tbct.compareTo(bigDecimal28) == 1) {
                                //当突变ct>=28的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal28) == -1 &&
                                    (ct3.compareTo(bigDecimal10) == 0 || ct3.compareTo(bigDecimal10) == -1)) {
                                //当26<=突变ct<28，并且德尔塔ct<=10的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 7:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal9) == 0 || ct3.compareTo(bigDecimal9) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=9的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 8:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal10) == 0 || ct3.compareTo(bigDecimal10) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=10的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 9:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal9) == 0 || ct3.compareTo(bigDecimal9) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=9的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 10:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal10) == 0 || ct3.compareTo(bigDecimal10) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=10的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 11:
                            if (tbct.compareTo(bigDecimal29) == 0 || tbct.compareTo(bigDecimal29) == 1) {
                                //当突变ct>=29的时候，为阴
                                c.setYyx("阴");
                                break;
                            }
                            if ((bigDecimal26.compareTo(tbct) == 0 || bigDecimal26.compareTo(tbct) == -1) && tbct.compareTo(bigDecimal29) == -1 &&
                                    (ct3.compareTo(bigDecimal9) == 0 || ct3.compareTo(bigDecimal9) == -1)) {
                                //当26<=突变ct<29，并且德尔塔ct<=9的时候，为弱阳
                                c.setYyx("弱阳");
                            }
                            break;
                        case 12:
                            break;
                        default:
                            break;
                    }
                }
                return c;
            }).collect(Collectors.toList());
            collect3.forEach(c -> experimentProcessResultDetailService.insert(c));
        });

        return true;
    }

}